package com.android.job.ui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.job.fusion.FusionCode;
import com.android.job.fusion.FusionField;
import com.android.job.ui.adapter.ItemAdapter;
import com.android.job.util.json.JSONArray;
import com.android.job.util.json.JSONException;

public class SelectActivity extends BaseActivity implements OnItemClickListener
{ 

	private ListView select_list;
	private ItemAdapter itemAdapter;
	private TextView item_title;
	private String[] codes;
	private String[] strings;
	private int view_id;
	private int type;
	private Button search_back;
	private boolean start = false;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select);
		type = getIntent().getIntExtra("type", 0);
		view_id = getIntent().getIntExtra("view_id", 0);
		select_list = (ListView) findViewById(R.id.select_list);
		item_title = (TextView) findViewById(R.id.item_title);
		search_back = (Button) findViewById(R.id.search_back);
		search_back.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View arg0)
			{
				if(type != FusionCode.JOB_TYPE_FROM_SHARE_ITEM){
					Intent data = new Intent();
					data.putExtra("chose", "");
					data.putExtra("view_id", 0);
					data.putExtra("text", "");
					setResult(0, data);
					finish();
					}else{
						Toast.makeText(SelectActivity.this, "必须选择一个职位",
								Toast.LENGTH_SHORT).show();
					}
			}
		});
		item_title.setText("请选择");
		if (checkType(type) != null)
		{
			itemAdapter = new ItemAdapter(checkType(type), this);
		}
		select_list.setAdapter(itemAdapter);
		select_list.setOnItemClickListener(this);
	}

	private String[] checkType(int type)
	{
		switch (type)
		{
		case FusionCode.CITY_ITEM_ONE:
//			item_title.setText("");
			return getRegionOne();
		case FusionCode.CITY_ITEM_TWO:
//			item_title.setText("");
			return null;
		case FusionCode.CITY_ITEM_THREE:
//			item_title.setText("");
			return null;
		case FusionCode.SALARY_ITEM:
//			item_title.setText("");
			codes = this.getResources().getStringArray(R.array.salary_range_code);
			strings = this.getResources().getStringArray(R.array.salary_range);
			return strings;
		case FusionCode.JOB_TYPE_ITEM:
//			item_title.setText("");
			return getJobType("type.txt");
		case FusionCode.DEGREE_ITEM:
//			item_title.setText("");
			codes = this.getResources().getStringArray(R.array.degree_code);
			strings = this.getResources().getStringArray(R.array.degree);
			return strings;
		case FusionCode.TIME_ITEM:
			item_title.setText("");
			codes = this.getResources().getStringArray(R.array.time_code);
			strings = this.getResources().getStringArray(R.array.time);
			return strings;
		case FusionCode.GENDER_ITEM:
//			item_title.setText("");
			codes = this.getResources().getStringArray(R.array.gender_code);
			strings = this.getResources().getStringArray(R.array.gender);
			return strings;
		case FusionCode.NATURE_ITEM:
//			item_title.setText("");
			codes = this.getResources().getStringArray(R.array.nature_code);
			strings = this.getResources().getStringArray(R.array.nature);
			return strings;
		case FusionCode.JOB_TYPE_FROM_SHARE_ITEM:
//			item_title.setText("");
			try
			{
				sharedPreferences = getSharedPreferences(FusionField.POSTARR,
						Activity.MODE_PRIVATE);
				JSONArray array = new JSONArray(sharedPreferences.getString("postArr", ""));
				int num = array.length();
				codes = new String[num];
				strings  = new String[num];
				for(int i = 0;i<num;i++){
					codes[i]	= array.getJSONObject(i).getString("postId");
					strings[i]	 = array.getJSONObject(i).getString("postName");
				}

			} catch (JSONException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return strings;
		case FusionCode.CATEGORIES_ITEM:
//			item_title.setText("");
			codes = this.getResources().getStringArray(R.array.categories_code);
			strings = this.getResources().getStringArray(R.array.categories);
			return strings;
		case FusionCode.POLITICS_ITEM:
//			item_title.setText("");
			codes = this.getResources().getStringArray(R.array.politics_code);
			strings = this.getResources().getStringArray(R.array.politics);
			return strings;
			
		}
		return null;
	}
	
	
	private String[] getJobType(String file){
		try
		{
		 InputStream is = this.getResources().getAssets().open(file);
		 InputStreamReader inputReader = new InputStreamReader(is ,"UTF-8");
         BufferedReader bufReader = new BufferedReader(inputReader);
         String line="";
         int num = 0;
         StringBuffer result= new StringBuffer();
         while((line = bufReader.readLine()) != null){
        	 result.append(line);
        	 result.append(";");
        	 num++;
         }
         
         strings = new String[num];
         codes = new String[num];
         String[] items = result.toString().split(";");
         String[] temp = new String[2];
         for (int i = 0; i < items.length; i++)
		{
//			temp = items[i].split(" ");
			codes[i] = items[i].substring(0, 7);
			strings[i] = items[i].substring(8).trim();
		}
         
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return strings;
	}
	
	
	private String[] getRegionOne(){
		try
		{
		 InputStream is = this.getResources().getAssets().open("regionone.txt");
		 InputStreamReader inputReader = new InputStreamReader(is ,"UTF-8");
         BufferedReader bufReader = new BufferedReader(inputReader);
         String line="";
         int num = 0;
         StringBuffer result= new StringBuffer();
         while((line = bufReader.readLine()) != null){
        	 result.append(line);
        	 result.append(";");
        	 num++;
         }
         bufReader.close();
         inputReader.close();
         is.close();
         strings = new String[num];
         codes = new String[num];
         String[] items = result.toString().split(";");
         for (int i = 0; i < items.length; i++)
		{
//			temp = items[i].split(" ");
			codes[i] = items[i].substring(0, 6);
			strings[i] = items[i].substring(6).trim();
			Log.i("SelectActivity", "=====codes======"+codes.toString());
			Log.i("SelectActivity", "=====codes======"+strings.toString());
		}
         
         
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return strings;
	}
	
	private String[] getRegionTwo(String code){
		Log.i("SelectActivity", "=====codes======"+code);
		if(!("320000".equals(code))){
		try
		{
		 InputStream is = this.getResources().getAssets().open("regionall.txt");
		 InputStreamReader inputReader = new InputStreamReader(is ,"UTF-8");
         BufferedReader bufReader = new BufferedReader(inputReader);
         String line="";
         int num = 0;
         StringBuffer result= new StringBuffer();
         while((line = bufReader.readLine()) != null){
        	 if(isAdap(code, line)){
            	 result.append(line);
            	 result.append(";");
            	 num++;
        	 }
         }
         bufReader.close();
         inputReader.close();
         is.close();
         Log.i("SelectActivity", "=====result======"+result.toString());
         strings = new String[num];
         codes = new String[num];
         String[] items = result.toString().split(";");
         for (int i = 0; i < items.length; i++)
		{
			codes[i] = items[i].substring(0, 6);
			strings[i] = items[i].substring(6).trim();
		}
         
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		
		}else{
			codes = this.getResources().getStringArray(R.array.js_code);
			strings = this.getResources().getStringArray(R.array.js);
		}
		
		return strings;
	}
	
	
	private String[] getRegionThree(String code){
		Log.i("SelectActivity", "=====codes======"+code+"===");
		if(!("320700".equals(code))){
		try
		{
		 InputStream is = this.getResources().getAssets().open("regionall.txt");
		 InputStreamReader inputReader = new InputStreamReader(is ,"UTF-8");
         BufferedReader bufReader = new BufferedReader(inputReader);
         String line="";
         int num = 0;
         StringBuffer result= new StringBuffer();
         while((line = bufReader.readLine()) != null){
        	 isThreeAdap(code, line);
        	 if(start){
            	 result.append(line);
            	 result.append(";");
            	 num++;
        	 }
         }
         bufReader.close();
         inputReader.close();
         is.close();
         Log.i("SelectActivity", "=====result======"+result.toString());
         strings = new String[num-1];
         codes = new String[num-1];
         String[] items = result.toString().split(";");
         for (int i = 1; i < items.length; i++)
		{
			codes[i-1] = items[i].substring(0, 6);
			strings[i-1] = items[i].substring(6).trim();
		}
         
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		
		}else{
			codes = this.getResources().getStringArray(R.array.lyg_code);
			strings = this.getResources().getStringArray(R.array.lyg);
		}
		
		return strings;
	}
	

	
	private boolean isAdap(String code, String line){
		String head = code.substring(0,2);
		String head1 = line.substring(0, 2);
		String end = line.substring(4, 6);
		String middle = line.substring(2,4);
		if((head.equals(head1))&&(end.equals("00"))&&(!middle.equals("00"))&&(!middle.equals("99"))){
			return true;
		}
		return false;
	}
	
	
	private void isThreeAdap(String code, String line){
//		String head = code.substring(0,2);
//		String head1 = line.substring(0, 2);
//		String end = line.substring(4, 6);
//		String middle = line.substring(2,4);
		if(start){
			if(line.substring(4, 6).equals("00")){
				start = false;
		}
	   }
		
		if(line.contains(code)){
			start = true;
		}
		
//		if((head.equals(head1))&&(end.equals("00"))&&(!middle.equals("00"))&&(!middle.equals("99"))){
//			return true;
//		}
	}
	
	
	
	

	@Override
	public void onBackPressed()
	{
		if(type != FusionCode.JOB_TYPE_FROM_SHARE_ITEM){
		Intent data = new Intent();
		data.putExtra("chose", "");
		data.putExtra("view_id", 0);
		data.putExtra("text", "");
		setResult(0, data);
		finish();
		super.onBackPressed();
		}else{
			Toast.makeText(SelectActivity.this, "必须选择一个职位",
					Toast.LENGTH_SHORT).show();
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int chose, long arg3)
	{
		Log.e("free.lang","======chose======"+chose+"======type======"+type+"======codes[chose]======"+codes[chose]);
		if((type == FusionCode.CITY_ITEM_ONE)&&((chose > 4)||(chose == 0))){
			
			getRegionTwo(codes[chose]);
			ItemAdapter itemAdapter = new ItemAdapter(strings,SelectActivity.this);
			select_list.setAdapter(itemAdapter);
			itemAdapter.notifyDataSetChanged();
			type = FusionCode.CITY_ITEM_TWO;
		}else if((type == FusionCode.CITY_ITEM_TWO)&&(getString(R.string.city_code).equals(codes[chose].trim()))){
//			codes = this.getResources().getStringArray(R.array.lyg_code);
//			strings = this.getResources().getStringArray(R.array.lyg);
			getRegionThree(getString(R.string.city_code));
			ItemAdapter itemAdapter = new ItemAdapter(strings,SelectActivity.this);
			select_list.setAdapter(itemAdapter);
			itemAdapter.notifyDataSetChanged();
			type = FusionCode.CITY_ITEM_THREE;
		}else if(type == FusionCode.JOB_TYPE_ITEM){
			switch (chose)
			{
			case 0:
				getJobType("type1.txt");
				break;
			case 1:
				getJobType("type2.txt");
				break;
			case 2:
				getJobType("type3.txt");
				break;
			case 3:
				getJobType("type4.txt");
				break;
			case 4:
				getJobType("type5.txt");
				break;
			case 5:
				getJobType("type6.txt");
				break;
			}
			ItemAdapter itemAdapter = new ItemAdapter(strings,SelectActivity.this);
			select_list.setAdapter(itemAdapter);
			itemAdapter.notifyDataSetChanged();
			type = FusionCode.JOB_TYPE_ITEM_TWO;
		}else{
			Intent data = new Intent();
			data.putExtra("chose", codes[chose]);
			data.putExtra("view_id", view_id);
			data.putExtra("text", strings[chose]);
			setResult(0, data);
			finish();
		}

	}
	
}
